<?php
 if(!isset($_SESSION['ma_cb']) || $_SESSION['ma_pq']!='admin')
    {
        header("Location: index.php?hc");
    }
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$countOK=0; 
$loingay=$soluong=null;
    if(isset($_POST['luu'])) {
//Nhập thư viện phpExcel vào.
        require_once ('thuvien/Classes/PHPExcel.php');
        require_once ('thuvien/Classes/PHPExcel/IOFactory.php');
//Lấy file import nếu file không bị lỗi.
        if($_FILES['file_import']['error'] == 0){
            $data = PHPExcel_IOFactory::load($_FILES['file_import']['tmp_name']);
            $sheetData = $data->getActiveSheet()->toArray(null, true, true, true);
            $matrung=array();
            
//Chạy từng hàng trong sheetData 
            foreach ($sheetData as $row) {
                if (!is_numeric($row['A'])) 
                    continue;
                    
                    
                    $sqlCheck="SELECT sbd_tt FROM tbl_tructuyen WHERE sbd_tt ='".$row['O']."'";
                    $exeCheck=mysql_query($sqlCheck);
                    
                    if(mysql_num_rows($exeCheck)>0) {
                        continue;
                    }else{                        
                          $cmtnd=str_replace("'","",$row['G']);
                            $dienthoai=str_replace("'","",$row['H']);
                            $ns=explode('/',$row['E']);                                                                                                                                                                                                    
                                if(count($ns)==3) 
                                    $ngaysinh="$ns[2]-$ns[1]-$ns[0]";
                                else
                                    $ngaysinh=null;
                            $ngnh=$row['V'];
                            $giophutgiay=substr($ngnh,0,8);
                            $ngaynhap=substr($ngnh,9,10); 
                            $nh=explode('/',$ngaynhap);                                    
                               if(count($nh)==3) 
                                    $ngaynhap="$nh[2]-$nh[1]-$nh[0] $giophutgiay";
                               else
                                    $ngaynhap=null;
                                                                             
                            $sqlInsert= "INSERT INTO tbl_tructuyen( sbd_tt, masv_tt, cmtnd_tt, hoten_tt, ten_tt, ngaysinh_tt, email_tt, gioitinh_tt, 
                                                                    dantoc_tt, tongiao_tt, dangvien_tt, noisinh_tt, bachoc_tt, namtotnghiep_tt, noitotnghiep_tt,
                                                                    hokhau_tt, diachi_tt, nangkhieu_tt, quequan_tt, dienthoai_tt, hotenbo_tt, ngaysinhbo_tt, 
                                                                    nghebo_tt, dienthoaibo_tt, hotenme_tt, ngaysinhme_tt, ngheme_tt, dienthoaime_tt, mon1_tt, 
                                                                    mon2_tt, mon3_tt, khoi_tt, nganh_tt, tongdiem_tt, khuvuc_tt, doituong_tt, ghichu_tt, trangthai_tt, 
                                                                    nguoinhap_tt,thoigiannhap_tt,chucvupt_tt) 
                                                           VALUES ( '".$row['O']."','".$row['B']."','".$cmtnd."','".$row['C']."','".$row['AK']."','".$ngaysinh."','".$row['I']."','".$row['D']."',
                                                                    '".$row['J']."','".$row['K']."','".$row['AL']."','".$row['F']."','".$row['L']."','".$row['M']."','".$row['N']."',
                                                                    '".$row['Y']."','".$row['X']."','".$row['AM']."','".$row['AN']."','".$dienthoai."','".$row['Z']."','".$row['AA']."',
                                                                    '".$row['AB']."','".$row['AC']."','".$row['AD']."','".$row['AE']."','".$row['AF']."','".$row['AG']."','".$row['P']."',
                                                                    '".$row['Q']."','".$row['R']."','".$row['AO']."','".$row['T']."','".$row['S']."','".$row['AH']."','".$row['AI']."','".$row['W']."','1',
                                                                    '".$row['U']."','".$ngaynhap."','".$row['AJ']."')";
                            $ex_insert=mysql_query($sqlInsert);
                            if($ex_insert)
                                $countOK++; 
                            
                            
                                                
                    }
            } //END of foreach
            unlink($_FILES['file_import']['tmp_name']);
        }
        $soluong="<font color=green><b>Đã thêm $countOK sinh viên </b></font>";
    }

        
$smarty->assign('soluong',$soluong);
$smarty->assign('loingay',$loingay);
//------------------------------------------------------------------------------------------------------------------------------
$smarty->display('tuyensinh/import_dssv.tpl');
?>